package springbook.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import springbook.enums.StatusEnum;
import springbook.mapper.BookInfoMapper;
import springbook.model.BookInfo;
import springbook.model.PageResult;
import springbook.model.RequestPage;

import java.util.List;
@Service
public class BookService {

    @Autowired
    private BookInfoMapper bookInfoMapper;

    //新增图书
    public Integer addBook(BookInfo bookInfo) {
        return bookInfoMapper.insert(bookInfo);
    }

    public Boolean queryBookExist(BookInfo bookInfo) {
        return bookInfoMapper.queryBookExist(bookInfo);
    }

    public PageResult<BookInfo> getListByPage(RequestPage requestPage) {
        //1.获取当前书籍的总数
        Integer count = bookInfoMapper.count();
        //2.获取对应页码的书籍信息
        List<BookInfo> bookInfos = bookInfoMapper.getBookInfoByPage(requestPage.getOffset(),requestPage.getPageSize());
        //3.根据数据库返回的信息设置书本信息(使用枚举)
        for(BookInfo x : bookInfos) {
            x.setStateCN(StatusEnum.getNameByCode(x.getStatus()).getName());
        }
        //4.返回结果
        return new PageResult<>(bookInfos,count,requestPage);
    }

    public BookInfo queryBookById(Integer bookId) {
        return bookInfoMapper.queryBookById(bookId);
    }
    public Integer updateBook(BookInfo bookInfo) {
        return bookInfoMapper.updateBook(bookInfo);
    }
    public Integer batchDelete(List<Integer> ids) {
        return bookInfoMapper.batchDelete(ids);
    }

}
